package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func main() {
	items := 100
	concurrency := 10
	in := make(chan int)
	ret := make(chan error)
	for x := 0; x < concurrency; x++ {
		wg.Add(1)
		go worker(in, ret)
	}

	go setIn(in, items)

	//wg.Wait()

	for err := range ret {
		if err != nil {
			fmt.Println(err.Error())
			break
		}
	}
}
func worker(in chan int, returnChan chan error) {
	//fmt.Printf("PROCESSING (%v)\n", x)
	defer wg.Done()
	for x := range in {
		if x == 95 {
			returnChan <- fmt.Errorf("Something not right")
		} else {
			returnChan <- nil
		}
	}
	returnChan <- fmt.Errorf("The End")
}

func setIn(in chan int, items int) {
	for x := 0; x < items; x++ {
		// loop over all items
		in <- x
	}
	close(in)
}
